package com.dfiles.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import android.app.Dialog;

import com.dfiles.utils.dialog.DialogUtil;

/**
 * 
 * @Description: From dhroid
 * @date 2015年8月6日 上午11:34:41
 */
public class ThreadWorker {
	static ExecutorService executorService;

	/**
	 * 线程池里跑runnable
	 * 
	 * @param runnable
	 * @return
	 */
	public static Future<?> executeRunalle(Runnable runnable) {
		if (executorService == null) {
			executorService = Executors.newFixedThreadPool(10);
		}
		return executorService.submit(runnable);
	}

	public static Future<?> execuse(boolean dialog, final Task task) {
		if (dialog) {
			Dialog pd = DialogUtil.showProgressDialog(task.mContext, "加载中......");
			pd.setCancelable(false);
			task.dialog = pd;
		}
		Future<?> future = executeRunalle(new Runnable() {
			@Override
			public void run() {
				try {
					task.doInBackground();
				} catch (Exception e) {
					e.printStackTrace();
					task.transfer(null, Task.TRANSFER_DOERROR);
					return;
				}
				task.transfer(null, Task.TRANSFER_DOUI);
			}
		});
		return future;
	}

}
